<?php
/**
 * Created by JetBrains PhpStorm.
 * User: DongVan
 * Date: 1/3/14
 * Time: 2:16 PM
 * To change this template use File | Settings | File Templates.
 */

namespace Post_facebook;
require_once(DIR_MODEL.'Comments.php');
date_default_timezone_set("Asia/Ho_Chi_Minh");
class Posts
{
    private $db = 'session';
    private $comment;
    private $debug;
    public function __construct()
    {
        if (!isset($_SESSION['db']) || empty($_SESSION['db'])) {
            die('lost database connection ...');
        }
        $this->db = $_SESSION['db'];
        $this->comment = new Comments();
        $this->debug = $_SESSION['debug'];
    }

    public function insert($arrayPost_facebook)
    {
        $id = $arrayPost_facebook['id'];
        if (isset ($arrayPost_facebook['picture'])) {
            $post_image = $arrayPost_facebook['picture'];
        } else {
            $post_image = "";
        }
        $post_uid = $arrayPost_facebook['from']['id'];
        $post_uname = $arrayPost_facebook['from']['name'];
        $post_uimage = "http://graph.facebook.com/" . $arrayPost_facebook['from']['id'] . "/picture";
        if (isset($arrayPost_facebook['message'])) {
            $post_message = str_replace('"','',$arrayPost_facebook['message']);
            $post_message = str_replace('\'','',$arrayPost_facebook['message']);
        } else {
            $post_message = "";
        }
        if (isset($arrayPost_facebook['likes'])) {
            $post_likes = sizeof($arrayPost_facebook['likes']['data']);
           // $this->debug->w($post_likes,1);
        }else{
            $post_likes = 0;
        }
        if (isset($arrayPost_facebook['comments'])) {
            $post_comments = sizeof($arrayPost_facebook['comments']['data']);
            //$this->debug->w($post_comments,1);
        }else{
            $post_comments = 0;
        }

        $post_created = date('Y/m/d h:i:s', strtotime($arrayPost_facebook['created_time']));
        $post_updated = date('Y/m/d h:i:s', strtotime($arrayPost_facebook['updated_time']));
        //$this->debug->w($post_updated,1);
        $sql = "INSERT INTO fb_posts SET post_id = '" . $id . "',
                                          post_image = '" . $post_image . "',
                                          post_uid = " . $post_uid . ",
                                          post_uname = '" . $this->db->escape($post_uname) . "',
                                          post_uimage = '" . $this->db->escape($post_uimage) . "',
                                          post_message = '" . mysql_real_escape_string($post_message) . "',
                                          post_likes = " . (int)$post_likes . ",
                                          post_comments = " . (int)$post_comments . ",
                                          post_created = '" . $post_created . "',
                                          post_updated = '" . $post_updated ."'
                                          ON DUPLICATE KEY UPDATE
                                                                  post_likes = ".(int)$post_likes.",
                                                                  post_comments = ".(int)$post_comments.",
                                                                  post_updated = '".$post_updated."',
                                                                  post_message = '".$post_message."'";
        $this->db->query($sql);
        //$this->debug->w($sql,0);
    }

    /* $data = array('start'=>0,
    'limit'=>10);*/
    public function getPostList($data = array())
    {



        $sql = " SELECT * FROM fb_posts WHERE 1=1 ";

        if(isset($data['hashtab'])){
            $sql .= " AND post_message LIKE '%".$this->db->escape($data['hashtab'])."%' ";
        }

        if(isset($data['sort'])){
            $sort = $data['sort'];
        }else{
            $sort = 'post_updated';
        }

        if(isset($data['order'])){
            $order = $data['order'];
        }else{
            $order = 'DESC';
        }

        if(!isset($data['limit']) || !isset($data['start'])){
            $start = 0;
            $limit = (int)DEFAULT_LIMIT;
        }else{
            $start = (int)$data['start'];
            $limit = (int)$data['limit'];
        }


        $sql .= " ORDER BY ".$sort." ".$order;
        $sql .= " LIMIT ".$start.",".$limit;

        $result = $this->db->query($sql)->rows;
        foreach($result as  &$item){
                if((int)$item['post_comments'] > 0){

                    $item['list_comments'] = $this->comment->getCommentByPostID($data,$item['post_id']);
                    //$this->debug->w($item['post_comments'],0);
                    //$this->debug->w($item,1);
                    //$item[$index]['post_comments'] = $this->comment->getCommentByPostID($item['post_id']);
                    //$this->debug->w($this->comment->getCommentByPostID($item['post_id']),1);
            }
        }

        //$this->debug->w($result,1);
        return !empty($result) ? $result : array();

    }
}

?>